package com.ethink.platform.manager;

import com.ethink.mdm.api.component.MdmComponent;
import com.ethink.mdm.api.info.CurrencyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ethink.order.api.constant.OrderConstant.*;
import static com.ethink.order.api.constant.OrderConstant.Z101;

/**
 * @author default
 * @version 1.0.0
 * @ClassName PublicManager.java
 * @Description
 * @createTime 2022年12月10日 12:08:00
 */
@Component
public class PublicManager {


    /**
     * 毛利率计算
     *
     * @return
     */
    public BigDecimal grossProfitRate(BigDecimal salePrice, BigDecimal costPrice) {
        if (salePrice == null || costPrice == null) {
            return BigDecimal.ZERO;
        }
        return salePrice.subtract(costPrice).divide(costPrice, 6, BigDecimal.ROUND_HALF_DOWN);
    }

    public BigDecimal multiply(BigDecimal var1, BigDecimal var2) {
        if (var1 == null || var2 == null) {
            return null;
        }
        return var1.multiply(var2).setScale(6, BigDecimal.ROUND_DOWN);
    }

    public Map<String, BigDecimal> saleDemandPriceCal(BigDecimal exceptSalePriceTax
            , BigDecimal exchangeRate, BigDecimal taxRate, BigDecimal discount) {
        Map<String, BigDecimal> map = new HashMap<>(4);
        BigDecimal noTax = exceptSalePriceTax.divide(BigDecimal.ONE.add(taxRate.divide(new BigDecimal("100")))
                , 6, BigDecimal.ROUND_HALF_UP).multiply(discount);
        map.put("noTax", noTax);
        map.put("taxLocal", multiply(exceptSalePriceTax, exchangeRate));
        map.put("noTaxLocal", multiply(noTax, exchangeRate));
        return map;
    }

    public BigDecimal getRate(String saleSubChannel) {
        switch (saleSubChannel) {
            case Z102:
                return new BigDecimal("0.99");
            case Z103:
                return new BigDecimal("0.97");
            case Z120:
                return new BigDecimal("0.96");
            case Z101:
                return new BigDecimal("0.98");
            default:
                return new BigDecimal("1");
        }

    }

    @Autowired
    private MdmComponent mdmComponent;

    public BigDecimal getExchangeRate(String currencyCode) {
        CurrencyInfo currency = mdmComponent.getCurrency(currencyCode);
        return currency.getUsdRate();
    }

    /**
     * @param saleSubChannel
     * @return 返回中的税率百分比值
     */
    public Map<String, BigDecimal> getTaxRateAndDiscount(String saleSubChannel) {
        Map<String, BigDecimal> map = new HashMap<>();
        switch (saleSubChannel) {
            //京东国际
            case "Z103":
                map.put("taxRate", BigDecimal.ZERO);
                map.put("discount", new BigDecimal("0.97"));
                break;
            //唯品会
            case "Z102":
                map.put("taxRate", BigDecimal.ZERO);
                map.put("discount", new BigDecimal("0.99"));
                break;
            //天猫国际
            case "Z101":
                map.put("taxRate", BigDecimal.ZERO);
                map.put("discount", new BigDecimal("0.98"));
                break;
            //小红书
            case "Z128":
                //得物
            case "Z146":
                //意欣
            case "Z000":
                //抖音进口超市
            case "Z153":
                //海拍客
            case "Z152":
                //淘宝
            case "Z100":
                //爱库存
            case "Z150":
                //行云
            case "Z151":
                map.put("taxRate", BigDecimal.ZERO);
                map.put("discount", new BigDecimal("1"));
                break;
            //快手
            case "Z143":
                //抖音
            case "Z142":
                //拼多多
            case "Z104":
                map.put("taxRate", new BigDecimal("9.1"));
                map.put("discount", new BigDecimal("1"));
                break;
            case "Z120":
                map.put("taxRate", BigDecimal.ZERO);
                map.put("discount", new BigDecimal("0.96"));
                break;
            default:
                map.put("taxRate", BigDecimal.ZERO);
                map.put("discount", new BigDecimal("1"));
                break;
        }
        return map;
    }

    public BigDecimal noTaxCount(BigDecimal price, BigDecimal taxRate, BigDecimal disCount) {
        if (price == null || taxRate == null || disCount == null) {
            return null;
        }
        return price.divide(BigDecimal.ONE.add(taxRate.divide(new BigDecimal("100"))), 6, BigDecimal.ROUND_HALF_UP).multiply(disCount);
    }

    /**
     * 根据渠道名称获取code
     *
     * @param name
     * @return
     */
    public String getSaleSubChannel(String name) {
        switch (name) {
            case "京东国际":
                return Z103;
            case "唯品会":
                return Z102;
            case "天猫国际":
                return Z101;
            case "小红书":
                return Z128;
            case "得物":
                return Z146;
            case "快手":
                return Z143;
            case "意欣":
                return Z000;
            case "抖音进口超市":
                return Z153;
            case "拼多多":
                return Z104;
            case "海拍客":
                return Z152;
            case "淘宝":
                return Z100;
            case "爱库存":
                return Z150;
            case "考拉":
                return Z120;
            case "行云":
                return Z151;
            default:
                return Z000;

        }
    }


    public List<String> getDiff(List<String> data1, List<String> data2) {
        return data1.stream().filter(item -> !data2.contains(item)).collect(Collectors.toList());
    }
}
